瀏覽器自動化(2) 同時調用Selenium2與Selenium3

虛擬還境可將python的模塊依賴分開管理,能使用指定版本或避免衝突問題。

Selenium2

使用技巧:

  1. firefox 不須透過webdriver。
  2. 默認支持全屏幕截圖。

如何調用

  1. 建立虛擬環境 Selenium2 VEnv

    安裝指定版本

    1
    pip install selenium==2.53.2
  2. 下載免安裝的firefox46.01

FirefoxPortable 46 下載地址

使用腳本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2018-05-16 08:33:47
# @Author : Maliao
# @Link : None

from selenium import webdriver
from PIL import Image
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from time import sleep

binary = FirefoxBinary(r".\FirefoxPortable\FirefoxPortable.exe") # firefox 主程序 檔案
profile = FirefoxProfile(r'.\FirefoxPortable\Data\profile') # firefox 使用者設定檔

browser = webdriver.Firefox(firefox_binary=binary, firefox_profile=profile)
browser.set_window_size(800, 900) # 指定窗口大小


# 依照元素大小截圖
def crop_screenshot(fullfile, cropfile, element):
browser.save_screenshot(fullfile)
if element:
type = element["type"]
name = element["name"]

imgelement = browser.find_element_by_xpath(".//*[@%s=%r]" % (type, name))
location = imgelement.location
size = imgelement.size
rangle = (int(location['x']), int(location['y']), int(
location['x'] + size['width']), int(location['y'] + size['height']))

i = Image.open(fullfile)
fincrop = i.crop(rangle)
fincrop.save(cropfile)
else:
pass

# 滾動窗口 加載所有圖片
def js_scroll_down(text):
jsdown = '''
(function () {
var y = 0;
var step = 100;
window.scroll(0, 0);

function f() {
if (y < document.body.scrollHeight) {
y += step;
window.scroll(0, y);
setTimeout(f, 100);
} else {
window.scroll(0, 0);
document.title += "%s";
}
}

setTimeout(f, 1000);
})();
''' % text

browser.execute_script(jsdown)


browser.get("https://www.ithome.com/html/android/360003.htm")

# 調用JS 下拉加載所有圖片
text = "SD"
js_scroll_down(text)
changetitle = "%s%s" % (browser.title, text)
while 1: # 死循環 等待js跑完
nowtitle = browser.title
if changetitle in nowtitle:
break
else:
sleep(1)


# 儲存 指定元素截圖
element = {"type": "class", "name": "post_content"}
cropfile = r"./data/ithome%s.png" % browser.current_url.split("/")[-1].split(".")[0]
fullfile = r"./data/ithomeCN.png"
crop_screenshot(fullfile=fullfile, cropfile=cropfile, element=element)

# 關閉瀏覽器
browser.quit()

截出一整張的長截圖。

selenium3

使用技巧:

  1. 支持最新版瀏覽器。
  2. 需使用webdriver驅動。

如何調用

  1. 建立虛擬環境 Selenium3 VEnv

    安裝最新版本

    1
    pip install selenium
  2. 下載Chrome

  3. 下載Chome Driver

使用腳本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2018-05-16 08:33:47
# @Author : Maliao
# @Link : None


from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Chrome() # 調用webdriver

browser.get("http://www.google.com")

browser.find_element_by_id("lst-ib").send_keys("python") # 搜索框輸入:"python"

browser.find_element_by_name("btnK").send_keys(Keys.ENTER) # 提交搜尋

titles = browser.find_elements_by_xpath(".//h3[@class='r']//a") # 搜尋所有標題

print([i.text for i in titles]) # 打印所有標題

browser.quit() # 離開瀏覽器

# 輸出:
['Welcome to Python.org', 'Python教程- 廖雪峰的官方网站', 'Python - 维基百科,自由的百科全书', 'Python 简介| 菜鸟教程', '教程一览| 莫烦Python', 'Python For Loops - W3Schools', 'Python Tutorial - W3Schools', 'Python - Attorneys at law – Geneva Switzerland Tokyo Brussels', 'Python Functions | DataCamp', '用Python 理財:打造小資族選股策略- Hahow 好學校']

小結:

操作上Chrome快速很多,而firefox雖然啟動比較慢,但不用webdriver及全屏截圖,實在相當便利。